//
//  MarqueeView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/29.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class MarqueeView: UIView {

    var timer : Timer!
    let firstLabel = UILabel()
//    let secondLabel = UILabel()
    
    override init(frame:CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.marqueeColor()
        
        firstLabel.text = marqueeText
        firstLabel.textColor = UIColor.red
        firstLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        
//        secondLabel.text = marqueeText
//        secondLabel.textColor = UIColor.red
//        secondLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        
        
        self.addSubview(firstLabel)
//        self.addSubview(secondLabel)
        
        firstLabel.frame = .init(x: ScreenW, y: 0, width: marqueeText.size(font: RATIO(maxNum: 14), height: RATIO_H(maxNum: 40)).width, height: RATIO_H(maxNum: 40))
        
//        secondLabel.frame = .init(x: firstLabel.frame.maxX + RATIO(maxNum: 20), y: 0, width: firstLabel.frame.size.width, height: RATIO_H(maxNum: 40))
        
        addAnimate()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 添加动画
    fileprivate func addAnimate() {

        timer = Timer.scheduledTimer(timeInterval: 0.02, target: self, selector: #selector(beginAnimation(sender:)), userInfo: nil, repeats: true)
        //添加至子线程
        RunLoop.main.add(self.timer, forMode: .commonModes)
    }
    
    //MARK: 开始动画
    @objc fileprivate func beginAnimation(sender:Timer) {
        
        firstLabel.frame.origin.x -= ScreenW/500
        if firstLabel.frame.maxX <= 0 {
            firstLabel.frame.origin.x = ScreenW
        }
    }
    
}
